package com.david.core.utils;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RequiresApi;

/**
 * Created by cuiweixing on 2019/8/14.
 *
 * example:
 * view.setForeground(new StateListDrawableHelper.Builder().addDrawable(StateListDrawableHelper.STATE_PRESSED_INT, R.mipmap.ic_home_big_fg).build())
 * TODO 添加渐变支持
 */
public class StateListDrawableHelper extends StateListDrawable {
    public static Application context;//设置ApplicationContext避免内存泄漏
    //states,取负值就表示pressed为false的意思: -STATE_PRESSED_INT
    public static final int STATE_PRESSED_INT = android.R.attr.state_pressed;
    public static final int STATE_SELECT_INT = android.R.attr.state_selected;
    public static final int STATE_ENABLE_INT = android.R.attr.state_enabled;
    public static final int STATE_CHECKED_INT = android.R.attr.state_checked;

    public static void init(Application application) {
        if (context == null) {
            context = application;
        }
    }

    public static class Builder {
        private List<StateBean> states = new ArrayList<>();
        private int strokeColor, strokeWidth;

        public Builder setStroke(int colorId, int strokeWidth) {
            setStroke(context, colorId, strokeWidth);
            return this;
        }

        public Builder setStroke(String colorString, int strokeWidth) {
            this.strokeColor = Color.parseColor(colorString);
            this.strokeWidth = strokeWidth;
            for (StateBean item : states) {
                if (item.drawable instanceof GradientDrawable) {
                    ((GradientDrawable) item.drawable).setStroke(strokeWidth, strokeColor);
                }
            }
            return this;
        }

        public Builder setStroke(Context context, int colorId, int strokeWidth) {
            this.strokeColor = context.getResources().getColor(colorId);
            this.strokeWidth = strokeWidth;
            for (StateBean item : states) {
                if (item.drawable instanceof GradientDrawable) {
                    ((GradientDrawable) item.drawable).setStroke(strokeWidth, strokeColor);
                }
            }
            return this;
        }

        /**
         * 添加图片drawable
         *
         * @param state      状态
         * @param drawableId 图片id
         * @return Builder
         */
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public Builder addDrawable(int state, int drawableId) {
            addDrawable(context, state, drawableId);
            return this;
        }

        /**
         * 添加图片drawable
         * @param context    context
         * @param state      状态
         * @param drawableId 图片id
         * @return Builder
         */
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public Builder addDrawable(Context context, int state, int drawableId) {
            states.add(new StateBean(new int[]{state}, context.getResources().getDrawable(drawableId, null)));
            return this;
        }

        /**
         * 添加颜色drawable
         * @param state   状态
         * @param colorId 颜色id
         * @return Builder
         */
        public Builder addColor(int state, int colorId) {
            addColor(context, state, colorId);
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param context context
         * @param state   状态
         * @param colorId 颜色id
         * @return Builder
         */
        public Builder addColor(Context context, int state, int colorId) {
            states.add(new StateBean(new int[]{state}, getColorDrawable(context.getResources().getColor(colorId))));
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param state   状态
         * @param colorId 颜色id
         * @param radius  四个圆角
         * @return Builder
         */
        public Builder addColor(int state, int colorId, int radius) {
            addColor(context, state, colorId, radius);
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param context context
         * @param state   状态
         * @param colorId 颜色id
         * @param radius  四个圆角
         * @return Builder
         */
        public Builder addColor(Context context, int state, int colorId, int radius) {
            states.add(new StateBean(new int[]{state}, getColorDrawable(context.getResources().getColor(colorId), radius)));
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param state             状态
         * @param colorId           颜色id
         * @param topLeftRadius     左上圆角半径
         * @param topRightRadius    右上圆角半径
         * @param bottomRightRadius 右下圆角半径
         * @param bottomLeftRadius  左下圆角半径
         * @return Builder
         */
        public Builder addColor(int state, int colorId, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius) {
            addColor(context, state, colorId, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius);
            return this;
        }


        /**
         * 添加颜色drawable
         *
         * @param state       状态
         * @param colorString 颜色:#eeeeee
         * @return Builder
         */
        public Builder addColor(int state, String colorString) {
            addColor(context, state, colorString);
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param context     context
         * @param state       状态
         * @param colorString 颜色:#eeeeee
         * @return Builder
         */
        public Builder addColor(Context context, int state, String colorString) {
            states.add(new StateBean(new int[]{state}, getColorDrawable(Color.parseColor(colorString))));
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param state       状态
         * @param colorString 颜色id
         * @param radius      四个圆角
         * @return Builder
         */
        public Builder addColor(int state, String colorString, int radius) {
            addColor(context, state, colorString, radius);
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param context     context
         * @param state       状态
         * @param colorString 颜色id
         * @param radius      四个圆角
         * @return Builder
         */
        public Builder addColor(Context context, int state, String colorString, int radius) {
            states.add(new StateBean(new int[]{state}, getColorDrawable(Color.parseColor(colorString), radius)));
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param state             状态
         * @param colorString       颜色id
         * @param topLeftRadius     左上圆角半径
         * @param topRightRadius    右上圆角半径
         * @param bottomRightRadius 右下圆角半径
         * @param bottomLeftRadius  左下圆角半径
         * @return Builder
         */
        public Builder addColor(int state, String colorString, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius) {
            addColor(context, state, colorString, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius);
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param context           context
         * @param state             状态
         * @param colorId           颜色id
         * @param topLeftRadius     左上圆角半径
         * @param topRightRadius    右上圆角半径
         * @param bottomRightRadius 右下圆角半径
         * @param bottomLeftRadius  左下圆角半径
         * @return Builder
         */
        public Builder addColor(Context context, int state, int colorId, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius) {
            states.add(new StateBean(new int[]{state}, getColorDrawable(context.getResources().getColor(colorId), topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius)));
            return this;
        }

        /**
         * 添加颜色drawable
         *
         * @param context           context
         * @param state             状态
         * @param colorString       颜色十六进制：#eeeeee
         * @param topLeftRadius     左上圆角半径
         * @param topRightRadius    右上圆角半径
         * @param bottomRightRadius 右下圆角半径
         * @param bottomLeftRadius  左下圆角半径
         * @return Builder
         */
        public Builder addColor(Context context, int state, String colorString, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius) {
            states.add(new StateBean(new int[]{state}, getColorDrawable(Color.parseColor(colorString), topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius)));
            return this;
        }

        public StateListDrawable build() {
            //注意该处的顺序，只要有一个状态与之相配，背景就会被换掉
            //所以不要把大范围放在前面了，如果sd.addState(new[]{},normal)放在第一个的话，就没有什么效果了
            StateListDrawable sd = new StateListDrawable();
            for (StateBean item : states) {
                sd.addState(item.state, item.drawable);
            }
            //必选添加在最后一个状态
//            sd.addState(new int[]{}, getStateDrawable(State.NORMAL));
            return sd;
        }

        private Drawable getColorDrawable(int color) {
            return getColorDrawable(color, 0);
        }

        private Drawable getColorDrawable(int color, int radius) {
            GradientDrawable shape = new GradientDrawable();
            shape.setCornerRadius(radius);//设置圆角大小
            shape.setColor(color);
            //参数含义: 边线宽度;边线颜色;虚线长度dashWidth;虚线空格长度dashGap
//            shape.setStroke(2, Color.BLACK, 5, 3);
            if (strokeColor != 0) {
                shape.setStroke(strokeWidth, strokeColor);
            }
            return shape;
        }

        private Drawable getColorDrawable(int color, int topLeftRadius, int topRightRadius, int bottomRightRadius, int bottomLeftRadius) {
            GradientDrawable shape = new GradientDrawable();
            shape.setCornerRadii(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius,
                    bottomLeftRadius});//设置圆角大小
            shape.setColor(color);
            //参数含义: 边线宽度;边线颜色;虚线长度dashWidth;虚线空格长度dashGap
//            shape.setStroke(2, Color.BLACK, 5, 3);
            if (strokeColor != 0) {
                shape.setStroke(strokeWidth, strokeColor);
            }
            return shape;
        }
    }

    static class StateBean {
        public StateBean(int[] state, Drawable drawable) {
            this.state = state;
            this.drawable = drawable;
        }

        private int[] state;
        private Drawable drawable;
    }
}